package com.xin.mmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xin.mmall.entity.Product;
import com.xin.mmall.entity.ProductCategory;
import com.xin.mmall.mapper.ProductCategoryMapper;
import com.xin.mmall.mapper.ProductMapper;
import com.xin.mmall.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.mmall.service.ProductService;
import com.xin.mmall.vo.ProductCategoryVO;
import com.xin.mmall.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴玉鑫
 * @since 2021-05-13
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<ProductCategoryVO> getAllProductCategoryVO() {


        //一级分类
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("type",1);
        List<ProductCategory> levelOne  = productCategoryMapper.selectList(wrapper);

            //转换成vo
//        List<ProductCategoryVO> levelOneVO = new ArrayList<>();
//        for (ProductCategory productCategory : levelOne) {
//            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
//            BeanUtils.copyProperties(productCategory,productCategoryVO);
//            levelOneVO.add(productCategoryVO);
//        }
        List<ProductCategoryVO> levelOneVo = levelOne.stream().
                map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());
        //图片赋值
        // 商品信息赋值
        for (int i = 0; i < levelOneVo.size(); i++) {
            levelOneVo.get(i).setBannerImg("/images/banner"+i+".png");
            levelOneVo.get(i).setTopImg("/images/top"+i+".png");

            QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
            productQueryWrapper.eq("categorylevelone_id",levelOneVo.get(i).getId());
            List<Product> products = productMapper.selectList(productQueryWrapper);
            //将products转换从vo类型
            List<ProductVO> productVOList = products.stream().
                    map(e -> new ProductVO(e.getId(),e.getName(),e.getPrice(),e.getFileName())).collect(Collectors.toList());
            levelOneVo.get(i).setProductVOSList(productVOList);
        }
        for (ProductCategoryVO levelOneProductCategoryVO : levelOneVo) {
            wrapper = new QueryWrapper<>();
            wrapper.eq("type",2);
            wrapper.eq("parent_id",levelOneProductCategoryVO.getId());
            List<ProductCategory> levelTwo = productCategoryMapper.selectList(wrapper);
            List<ProductCategoryVO> levelTwoVO = levelTwo.stream().
                    map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());
            levelOneProductCategoryVO.setChildren(levelTwoVO);
            for (ProductCategoryVO levelTwoProductCategoryVO : levelTwoVO) {
                wrapper = new QueryWrapper<>();
                wrapper.eq("type",3);
                wrapper.eq("parent_id",levelTwoProductCategoryVO.getId());
                List<ProductCategory> levelThree = productCategoryMapper.selectList(wrapper);
                List<ProductCategoryVO> levelThreeVO = levelThree.stream().
                        map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());
                levelTwoProductCategoryVO.setChildren(levelThreeVO);
            }
        }
        return levelOneVo;
    }
}
